package org.zlm.git_study.util;

import lombok.extern.slf4j.Slf4j;
import org.zlm.git_study.annotation.UnitBigDecConvert;
import org.zlm.git_study.enums.UnitConvertType;
import org.zlm.git_study.pojo.SumReport;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 单位装换工具
 *
 * @author zlm
 * @date 2024/1/4
 */
@Slf4j
public class UnitConvertUtil {

    public static <T> void unitMapConvert(List<T> list, Map<String, UnitConvertType> propertyMap){
        list.forEach(item -> {
            //获取所有属性
            Field[] fields = item.getClass().getDeclaredFields();
            for (Field field : fields) {
                if(propertyMap.containsKey(field.getName())){
                    try {
                        //设置属性可访问
                        field.setAccessible(true);
                        //获取属性值
                        Object value = field.get(item);
                        UnitConvertType unitConvertType = propertyMap.get(field.getName());
                        if(value != null){
                            BigDecimal bigDecimal;
                            if(unitConvertType == UnitConvertType.PERCENTAGE){
                                bigDecimal = ((BigDecimal) value).multiply(BigDecimal.valueOf(100));
                                field.set(item,bigDecimal);
                            }else if(unitConvertType == UnitConvertType.PERMIL){
                                bigDecimal = ((BigDecimal) value).multiply(BigDecimal.valueOf(1000));
                                field.set(item,bigDecimal);
                            }else if(unitConvertType == UnitConvertType.TEN_THOUSAND_YUAN){
                                bigDecimal = ((BigDecimal) value).divide(BigDecimal.valueOf(10000)).setScale(2, RoundingMode.HALF_UP);
                                field.set(item,bigDecimal);
                            }else if(unitConvertType == UnitConvertType.ACCURACY){
                                bigDecimal = ((BigDecimal) value).setScale(2, RoundingMode.HALF_UP);
                                field.set(item,bigDecimal);
                            }else{
                                log.error("未知的单位转换类型");
                            }
                        }

                    }catch (Exception e){
                        log.error("属性转换失败",e);
                    }
                }
            }
        });

    }
    public static <T> void unitAnnotationConvert(List<T> list){
        list.forEach(item -> {
            Field[] fields = item.getClass().getDeclaredFields();
            for (Field field : fields) {
                try {
                    //获取注解
                    UnitBigDecConvert annotation = field.getAnnotation(UnitBigDecConvert.class);
                    if(annotation == null){
                        continue;
                    }
                    UnitConvertType name = annotation.name();
                    field.setAccessible(true);
                    Object o = field.get(item);
                    if(o != null){
                        if(name == UnitConvertType.PERCENTAGE){
                            BigDecimal bigDecimal = ((BigDecimal) o).multiply(BigDecimal.valueOf(100));
                            field.set(item,bigDecimal);
                        }else if(name == UnitConvertType.PERMIL){
                            BigDecimal bigDecimal = ((BigDecimal) o).multiply(BigDecimal.valueOf(1000));
                            field.set(item,bigDecimal);
                        }else if(name == UnitConvertType.TEN_THOUSAND_YUAN){
                            BigDecimal bigDecimal = ((BigDecimal) o).divide(BigDecimal.valueOf(10000)).setScale(2, RoundingMode.HALF_UP);
                            field.set(item,bigDecimal);
                        }else if (name == UnitConvertType.ACCURACY){
                            BigDecimal bigDecimal = ((BigDecimal) o).setScale(2, RoundingMode.HALF_UP);
                            field.set(item,bigDecimal);
                        }else {
                            log.error("未知的单位转换类型");
                        }
                    }
                } catch (IllegalAccessException e) {
                    log.error("装换失败",e);
                }
            }
        });
    }

    public static void main(String[] args) {
        SumReport sumReport = new SumReport();
        sumReport.setPayTotalAmount(new BigDecimal(2390000));
        sumReport.setJcAmountPer(BigDecimal.valueOf(0.885));
        sumReport.setJcCountPer(BigDecimal.valueOf(0.2394));
        sumReport.setLength(BigDecimal.valueOf(1700.64003));

        List<SumReport> list = new ArrayList<>();
        list.add(sumReport);

        unitAnnotationConvert(list);
        System.out.println(list);

    }
}
